package com.chennan.pattern.adapter.demo.passport;

import com.chennan.pattern.adapter.demo.passport.adapterimpl.LoginForQQAdapter;

/**
 * @author chennan
 * @create 2021-09
 */
public class PassportForThirdAdapter implements IPassportForThird{

    @Override
    public ResultMsg loginForQQ(String openId) {
        return processLogin(openId, LoginForQQAdapter.class);
    }

    @Override
    public ResultMsg loginForWx(String openId) {
        return processLogin(openId,null);
    }

    @Override
    public ResultMsg loginForTelPhone(String phone, String code) {
        return processLogin(phone,null);
    }

    @Override
    public ResultMsg loginForToken(String token) {
        return processLogin(token,null);
    }

    private ResultMsg processLogin(String id,Class<? extends IloginAdapter> clazz){
        try {
            IloginAdapter adapter = clazz.newInstance();
            if (adapter.support(adapter)) {
                return adapter.login(id,adapter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private ResultMsg loginForRegist(String username,String password){
        return ResultMsg.ok("");
    }
}
